package cloud.maque.admin.aop;

import cloud.maque.admin.annotations.TenantId;
import cloud.maque.admin.domain.consts.ConstVar;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cloud.maque.framework.security.ssoclient.utils.SsoUserInfoUtils;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.util.FieldUtils;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * 对数据的查询进行入注入
 *
 * @author zhuyf
 * @date 2023/5/13 13:32
 */
@Aspect
@Component
@Slf4j
public class TenantQueryAop {


    @Autowired
    SsoUserInfoUtils infoUtils;

    @Pointcut("@annotation(cloud.maque.admin.annotations.TenantQuery)")
    public void TenantQueryPoint() {

    }

    /**
     * 租户ID 根据用户当前的Token去修改租户ID,防止数据被篡改
     *
     * @param point
     * @throws IllegalAccessException
     */
    @Around("TenantQueryPoint()")
    public Object Around(ProceedingJoinPoint point) throws Throwable {

        //从会话中获取租户ID

        Object[] arguments = point.getArgs();
        if (arguments.length == 0) {
            Object result = point.proceed(arguments);
            return result;
        }

        //替换请求的租户ID
        String tenantId = infoUtils.getTenantId();

        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        Method method = methodSignature.getMethod();
        //参数注解，1维是参数，2维是注解
        Annotation[][] annotations = method.getParameterAnnotations();

        for (int i = 0; i < annotations.length; i++) {
            Object param = arguments[i];
            Annotation[] paramAnn = annotations[i];
            //参数为空，直接下一个参数
            if (paramAnn.length == 0) {
                continue;
            }
            //如果是分页，则直接设置
            if (arguments[i] instanceof PaginateDataResquest) {
                PaginateDataResquest paginateDataResquest = (PaginateDataResquest) arguments[i];
                if (paginateDataResquest.getData() != null) {
                    Field field1 = FieldUtils.getField(paginateDataResquest.getData().getClass(), ConstVar.TenantIdFieldName);
                    if (field1 != null) {
                        field1.setAccessible(true);
                        //如果是管理租户且值不为空，则不修改值
                        if (tenantId.equals(ConstVar.ManagerTenandId) && !StrUtil.isEmptyIfStr(field1.get(paginateDataResquest.getData()))) {
                            //不修改值
                        } else {
                            field1.set(paginateDataResquest.getData(), tenantId);
                        }
                    }
                }
            } else {
                for (Annotation annotation : paramAnn) {
                    //这里判断当前注解是否为TenantId.class
                    //把租户ID直接设置为用户的ID
                    if (annotation.annotationType().equals(TenantId.class)) {
                        arguments[i] = tenantId;
                    }
                }
            }
        }


        Object result = point.proceed(arguments);
        return result;

    }


}
